package com.example.tool.erp.demo;

import com.example.tool.erp.sdk.Client;
import com.example.tool.erp.sdk.Pager;
import com.example.tool.erp.sdk.WdtErpException;
import com.example.tool.erp.sdk.api.goods.GoodsClassAPI;
import com.example.tool.erp.sdk.api.goods.dto.GoodsClassSearchRequest;
import com.example.tool.erp.sdk.api.goods.dto.GoodsClassSearchResponse;
import com.example.tool.erp.sdk.impl.ApiFactory;
import com.example.tool.erp.sdk.impl.DefaultClient;

public class GoodsClass
{
//	public static void main(String[] args) throws WdtErpException
//	{
//		Client client = DefaultClient.get("wdterp30", "http://172.172.1.62:30000/", "xyx_api",
//				"e8866c1681dacc9488a19c89991791b8:8f5800dab84b7a8d614e92cba739032c");
//		GoodsClassAPI api = ApiFactory.get(client, GoodsClassAPI.class);
//
//		queryGoodsClass(api);
//	}

	private static void queryGoodsClass(GoodsClassAPI api) throws WdtErpException
	{
		int pageSize = 5;
		Pager pager = new Pager(pageSize, 0, true);
		GoodsClassSearchRequest request = new GoodsClassSearchRequest();

		request.setStartTime("2023-01-01");
		request.setEndTime("2023-02-01");

		GoodsClassSearchResponse response = api.search(request, pager);

		Integer total = response.getTotal();
		if (null == total || pageSize >= total)
		{
			System.out.println("处理数据");
			return;
		}

		int totalPage = (total % pageSize == 0 ? total / pageSize : total / pageSize + 1) - 1;
		pager.setCalcTotal(false); //后续翻页不需要计算总条数, 可以大大减少请求时间
		for (int i = totalPage; i >= 0; i--)// 从后向前翻页
		{
			pager.setPageNo(i);
			System.out.print("pager: page_size:" + pageSize + "  page_no: " + i + "  ");
			response = api.search(request, pager);
			System.out.println("处理数据");
		}
	}
}
